package com.leyou.item.service;

import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import com.leyou.item.pojo.SpecGroup;
import com.leyou.item.pojo.SpecParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: taft
 * @Date: 2018-9-29 15:47
 */
@Service
public class SpecificationService {

    @Autowired
    private SpecGroupMapper specGroupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    public List<SpecGroup> querySpecGroups(Long cid) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(cid);
        return specGroupMapper.select(specGroup);
    }

    public List<SpecParam> querySpecParams(Long gid, Long cid, Boolean searching, Boolean generic) {

        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);
        specParam.setGeneric(generic);
        return specParamMapper.select(specParam);
    }

    public List<SpecGroup> querySpecsByCid(Long cid) {




        //根据分类id查询对应的规格参数表中的所有的规格组信息
        List<SpecGroup> select = querySpecGroups(cid);

        select.forEach(group->{
            //迭代每个规格组信息，查询组内对应的规格参数
            SpecParam specParam = new SpecParam();

            specParam.setGroupId(group.getId());
            group.setParams(specParamMapper.select(specParam));
        });
        return select;
    }
}
